package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.store.contract.OneLotteryParticipateContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.OneLotteryParticipateBean;
import com.meitianhui.convenitenbuy.module.store.ui.activity.OneLotteryParticipateActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/27.
 */

public class OneLotteryParticipatePresenter implements OneLotteryParticipateContract.Presenter {

    private OneLotteryParticipateActivity mContext;
    private final OneLotteryParticipateContract.View mView;
    private Gson gson;

    public OneLotteryParticipatePresenter(OneLotteryParticipateActivity view,
                                          OneLotteryParticipateActivity context) {
        this.mView = view;
        this.mContext = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getParticipate(int page) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", page + "");
        maps.put("page_size", 10 + "");
        String pageParams = gson.toJson(maps);

        Map<String, String> map = new HashMap<>();
        map.put("consumer_id", token.getMember_id());
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .getOneLotteryParticipate(token.getUser_token(),
                        "goods.ldActivityProcessForConsumerPageFind", pageParams, params)
                .subscribe(new ResponseObserver<BaseResponse<OneLotteryParticipateBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<OneLotteryParticipateBean> response) {
                        mView.getParticipateBack(true, null, response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getParticipateBack(false, msg, null);
                    }
                });
    }

}
